package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.SliceGroup;
import com.ffcs.crmd.tsp.admin.service.ISliceGroupService;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.api.dto.client.TspSliceGroup;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:分片组srevice
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2015年10月20日 下午4:26:46
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Service
public class SliceGroupServiceImpl extends AbstractService implements ISliceGroupService {
    
    @Override
    public void deleteSliceGroup(List<SliceGroup> sliceGroupList) throws TspException {
       for(SliceGroup sliceGroup : sliceGroupList){
           deleteSliceGroup(sliceGroup);
       }
    }
    
    @Override
    public void deleteSliceGroup(SliceGroup sliceGroup) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            tspSliceGroupVO.setClusterName(sliceGroup.getClusterName());
            tspSliceGroupVO.setId(sliceGroup.getId());
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<?> tspResult = tspClient.deleteSliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public SliceGroup get(SliceGroup sliceGroup) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            BeanUtils.applyIf(tspSliceGroupVO, sliceGroup, false);
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<TspSliceGroup> tspResult = tspClient.querySliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
            
            SliceGroup rsliceGroup = new SliceGroup();
            BeanUtils.applyIf(rsliceGroup, tspResult.getRetVo().getObject(), false);
            return rsliceGroup;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public List<SliceGroup> query(SliceGroup sliceGroup) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            BeanUtils.applyIf(tspSliceGroupVO, sliceGroup, false);
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<TspSliceGroup> tspResult = tspClient.querySliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
            
            List<TspSliceGroup> tspSliceVOList = new ArrayList<TspSliceGroup>();
            tspSliceVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspSliceVOList, SliceGroup.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }

    @Override
    public PageInfo<SliceGroup> queryPage(SliceGroup sliceGroup, int pageNumber, int pageSize) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            BeanUtils.applyIf(tspSliceGroupVO, sliceGroup, false);
            tspSliceGroupVO.setPageNumber(pageNumber);
            tspSliceGroupVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<TspSliceGroup> tspResult = tspClient.querySliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspSliceGroup> tspSliceGroupPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<SliceGroup> sliceGroupPageInfo = new PageInfo<SliceGroup>();
            BeanUtils.applyIf(sliceGroupPageInfo, tspSliceGroupPageInfo, false);
            sliceGroupPageInfo.setList(BeanUtils.copyList(tspSliceGroupPageInfo.getList(), SliceGroup.class));
            return sliceGroupPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void saveSliceGroup(SliceGroup sliceGroup) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            BeanUtils.applyIf(tspSliceGroupVO, sliceGroup);
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<?> tspResult = tspClient.createSliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }

    
    @Override
    public void updateSliceGroup(SliceGroup sliceGroup) throws TspException {
        try {
            TspSliceGroup tspSliceGroupVO = new TspSliceGroup();
            BeanUtils.applyIf(tspSliceGroupVO, sliceGroup);
            
            TspClient tspClient = getClient(sliceGroup.getClusterName());
            TspResult<?> tspResult = tspClient.updateSliceGroup(tspSliceGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
